package com.mantru.iotpot.Cook;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.mantru.iotpot.ApplicationClass;
import com.mantru.iotpot.Cleaning.CleanWayActivity;
import com.mantru.iotpot.R;
import com.mantru.iotpot.Tools.LoadingDialogUtil;

public class PutFoodActivity extends AppCompatActivity {
    private ApplicationClass ac;
    private Handler handler;
    private Runnable runnable;
    private int countdown = 60; // 初始倒计时为60秒

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_finish_cook);
        ac = (ApplicationClass) getApplication(); // 初始化 ApplicationClass

        // 初始化倒计时文本
        TextView countdownTextView = findViewById(R.id.cook_finish_secound);
        countdownTextView.setText(countdown + "S");
        // 初始化 Handler 和 Runnable
        handler = new Handler();
        runnable = new Runnable() {
            @Override
            public void run() {
                // 每秒更新倒计时
                countdown--;
                if (countdown >= 0) {
                    runOnUiThread(() -> {
                        TextView countdownTextView = findViewById(R.id.cook_finish_secound);
                        countdownTextView.setText(countdown + "S");
                    });
                    Log.d("putfood","ac.pot_position_value: " + ac.pot_position_value);
                    // 判断 pot_position_value 是否为 2
                    if (ac.pot_position_value == 2) {
                        // 跳转到下一个界面
//                        LoadingDialogUtil.showLoadingDialog(PutFoodActivity.this);
                        Intent intent = new Intent(PutFoodActivity.this, FinishFoodActivity.class);
                        startActivity(intent);
                        finish(); // 关闭当前活动
                    } else if (countdown == 0) {
                        // 倒计时结束，跳转到下一个界面
//                        LoadingDialogUtil.showLoadingDialog(PutFoodActivity.this);
                        Intent intent = new Intent(PutFoodActivity.this, FinishFoodActivity.class);
                        startActivity(intent);
                        finish(); // 关闭当前活动
                    } else {
                        // 继续倒计时
                        handler.postDelayed(this, 1000);
                    }
                }
            }
        };

        // 启动倒计时
        handler.postDelayed(runnable, 1000);
    }

    @Override
    protected void onDestroy() {
        // 移除所有回调，避免内存泄漏
        if (handler != null) {
            handler.removeCallbacks(runnable);
        }
        super.onDestroy();
    }

//    private BroadcastReceiver receiver = new BroadcastReceiver() {
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            if ("ACTION_CLOSE_LOADING_DIALOG".equals(intent.getAction())) {
//                LoadingDialogUtil.closeLoadingDialog();
//            }
//        }
//    };

    @Override
    protected void onStart() {
        super.onStart();
//        LocalBroadcastManager.getInstance(this).registerReceiver(receiver, new IntentFilter("ACTION_CLOSE_LOADING_DIALOG"));
    }

    @Override
    protected void onStop() {
        super.onStop();
//        LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
    }

    @Override
    protected void onResume() {
        super.onResume();
//        // 在界面渲染完成后发送广播关闭弹窗
//        new Handler().post(new Runnable() {
//            @Override
//            public void run() {
//                ((ApplicationClass) getApplication()).sendBroadcast("ACTION_CLOSE_LOADING_DIALOG");
//            }
//        });
    }
}